<?php

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/cls_json.php');


if(empty($_REQUEST['goods_imgs'])){
	if(empty($_REQUEST['p'])||empty($_REQUEST['t']) )
	{
		die('请给参数p和t赋值，例如： p=init_product&t=pimg  其中init_product为文件夹名称！,pimg(商品图片)  , cimg(色卡), pare(搭配) ');
	}else{
	
		$IMG_ROOT_PATH =SOURCE_PATH.'images/'.$_REQUEST['p'].'/';
		echo "<h2>目录为粉红色</h2>"; 
		tree($IMG_ROOT_PATH,'images/'.$_REQUEST['p'],$_REQUEST['t']) ;
	}
}else{
	
	$sql = "select * from ".$GLOBALS['os']->table('goods');
	$goods_arr = $GLOBALS['db']->getAll($sql);
	
	foreach ($goods_arr as $goods_info){
		$good_id = $goods_info['goods_id'];
		
		//取得商品的所有的相册
		$sql = "select img_url from " . $GLOBALS['os']->table('goods_gallery') ." where goods_id = ".$good_id;
		
		$pics = $GLOBALS['db']->getAll($sql);
		
		$img_text = '';
		//更新商品
		foreach ($pics as $url){
			$img_text = $img_text."<img src=".$_CFG['pre_fetch_img'].$url['img_url']."?600 <br>";
		}
		
		$sql = "update ".$GLOBALS['os']->table('goods')." set goods_desc = goods_desc + '"
		.$img_text."' where goods_id = ".$good_id;

		$GLOBALS['db']->query($sql);
	}
	
}
function tree($directory,$rela_path,$t) 
{ 
	//第一步：递归文件夹
	$mydir=dir($directory); 

	echo "<ul>"; 
	while($file=$mydir->read()){ 

		if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!=".."))
		{ 
			echo "<li><font color='#ff00cc'><b>$file</b></font></li>"; 

			tree("$directory/$file",$rela_path.'/'.$file,$t); 
		}else{ 
			
//			echo "<li>  $rela_path/$file </li>";

			//第二步：读取文件名,分析出商品id，颜色id，排序号
			
			$temp_s = explode(".",$file);
			if(sizeof($temp_s) != 2)
			{
				echo "<li>  $rela_path/$file ... 文件名称规则有误 </li>";
				continue;
			}
			
			//取文件名前面部分处理
			$goods_infos = explode("_",$temp_s[0]);
			
			if( ($t == 'pimg') && (sizeof($goods_infos) == 3)) {//处理商品图片
				//商品sn
				$good_sn = $goods_infos[0];
				//颜色ID
				$color_code = $goods_infos[1];
				//排序ID
				$rank_id = $goods_infos[2];
				
				//第三步：数据库处理
				echo "<li>  $rela_path/$file ... "; 
				
				
				//取得商品信息
				$sql = "select * from ".$GLOBALS['os']->table('goods')." where goods_sn = '".$good_sn."'" ;
				$goods_info = $GLOBALS['db']->getAll($sql);
	            $good_id = $goods_info[0]['goods_id'];
	
				if(empty($good_id)){
					echo " 该商品不存在 </li>"; 
					continue;
				}
				
				//取得颜色id
				$sql = "select color_id from ".$GLOBALS['os']->table('color')." where color_code = '".$color_code."'" ;
	            $color_id = $GLOBALS['db']->getOne($sql);
				if(empty($color_id)){
					echo " 该颜色不存在 </li>"; 
					continue;
				}
				
				if($rank_id == "00")
				{
					//判断商品是否已经有主图
	//				if(empty($goods_info[0]['goods_img']))
	//				{}
					$sql = "UPDATE " . $GLOBALS['os']->table('goods') . " SET ";
					$sql = $sql." goods_thumb = '".$rela_path.'/'.$file."',";
					$sql = $sql." goods_img = '".$rela_path.'/'.$file."',";
					$sql = $sql." original_img = '".$rela_path.'/'.$file."'";
					$sql = $sql." where goods_id = ".$good_id;
					$GLOBALS['db']->query($sql);
				}
				
				//取出当前所有图片
				$is_existed = false;
				$sql = "select img_url from " . $GLOBALS['os']->table('goods_gallery') ." where goods_id = ".$good_id;
				$pics = $GLOBALS['db']->getAll($sql);

				foreach ($pics as $url){
					if($url['img_url'] == ($rela_path.'/'.$file)){
						$is_existed = true;
						break;
					}
				}
				if(!$is_existed){
					//插入商品相册表
					$sql = "insert into " . $GLOBALS['os']->table('goods_gallery') 
					. " ( goods_id,img_url,thumb_url,img_original,color_id,type,sort_order) values(" .
		             $good_id.",'".$rela_path.'/'.$file."','".$rela_path.'/'.$file."','".$rela_path.'/'.$file."',"
		             .$color_id.",1,".intval($rank_id)
		            .")" ;
					$GLOBALS['db']->query($sql);
				}
	
				echo " .... 商品图片处理完毕 ... </li>"; 
				

			}elseif ( ($t == 'cimg')&&(sizeof($goods_infos) == 2)){//处理颜色

				//颜色编码
				$color_code = $goods_infos[1];
				
				$sql = " update ".$GLOBALS['os']->table('color')." set img_file='".
				$rela_path.'/'.$file."' where color_code = '".$color_code."'" ;
	            
				$GLOBALS['db']->query($sql);
				
				echo "<li>  $rela_path/$file ... 颜色处理完毕 </li>"; 
				
			}elseif (($t == 'pare') && (sizeof($goods_infos) > 3)){//处理搭配
				
				print_r($goods_infos);
				$goods_arr = array();
				$goods_ids = array();
				for ($i=0 ; $i<(sizeof($goods_infos)-1) ; $i++){
					
						if($i%2 == 0){
							$goods_arr[intval($i/2)]['goods_sn']=$goods_infos[$i];
						}else{
							$goods_arr[(intval($i-1)/2)]['color_sn']=$goods_infos[$i];
						}
					
				}

				$count = 0;
				foreach ($goods_arr as $temp_good){
					//将该商品依次插入相关的商品颜色相册下面
					//取得商品id
					$sql = "select * from ".$GLOBALS['os']->table('goods')." where goods_sn = '".$temp_good['goods_sn']."'" ;
					$goods_info = $GLOBALS['db']->getAll($sql);
		            $good_id = $goods_info[0]['goods_id'];
		
					if(empty($good_id)){
						echo " 该商品不存在 </li>"; 
						continue;
					}
					$goods_ids[$count] = $good_id;
					$count++;
					
					//取得颜色id
					$sql = "select color_id from ".$GLOBALS['os']->table('color')." where color_code = '".$temp_good['color_sn']."'" ;
		            $color_id = $GLOBALS['db']->getOne($sql);
					if(empty($color_id)){
						echo " 该颜色不存在 </li>"; 
						continue;
					}
					
					//取出当前所有图片
					$is_existed = false;
					$sql = "select img_url from " . $GLOBALS['os']->table('goods_gallery') ." where goods_id = ".$good_id;
					$pics = $GLOBALS['db']->getAll($sql);
					
					foreach ($pics as $url){
						if($url['img_url'] == ($rela_path.'/'.$file)){
							$is_existed = true;
							break;
						}
					}
					if(!$is_existed){
						//插入商品相册表
						$sql = "insert into " . $GLOBALS['os']->table('goods_gallery') 
						. " ( goods_id,img_url,thumb_url,img_original,color_id,type,sort_order) values(" .
			             $good_id.",'".$rela_path.'/'.$file."','".$rela_path.'/'.$file."','".$rela_path.'/'.$file."',"
			             .$color_id.",1,2)";
						$GLOBALS['db']->query($sql);
					}
				}
				
				insert_group_goods($goods_ids);

				echo "<li>  $rela_path/$file ... 搭配处理完毕 </li>"; 
				
			}

		} 
	} 
	echo "</ul>"; 
	$mydir->close(); 
} 

function insert_group_goods($goods_ids){
	
	$goods_pare = array();
	$count = 0;
	for($i=0;$i<sizeof($goods_ids);$i++){
		for($j=0;$j<sizeof($goods_ids);$j++){
			$goods_pare[$count]['parent']=$goods_ids[$i];
			$goods_pare[$count]['child']=$goods_ids[$j];
		}
	}
	
	foreach ($goods_pare as $pare){
		if($pare['parent'] == $pare['child']) continue;
		
		$sql = " select count(*) from ".$GLOBALS['os']->table('group_goods')." where parent_id = ".$pare['parent'].
		" and goods_id = ".$pare['child'] ;
        $count = $GLOBALS['db']->getOne($sql);
        if($count == 0){
        	$sql = " insert into  ".$GLOBALS['os']->table('group_goods')." (parent_id,goods_id,goods_price,admin_id)".
        	"values(".$pare['parent'].",".$pare['child'].",0,0) ";
        	$GLOBALS['db']->query($sql);
        }
	}
}

function vote_already_submited($vote_id, $ip_address)
{
    $sql = "SELECT COUNT(*) FROM ".$GLOBALS['os']->table('vote_log')." ".
           "WHERE ip_address = '$ip_address' AND vote_id = '$vote_id' ";

    return ($GLOBALS['db']->GetOne($sql) > 0);
}

function save_vote($vote_id, $ip_address, $option_id)
{
    $sql = "INSERT INTO " . $GLOBALS['os']->table('vote_log') . " (vote_id, ip_address, vote_time) " .
           "VALUES ('$vote_id', '$ip_address', " . gmtime() .")";
    $res = $GLOBALS['db']->query($sql);

    /* 更新投票主题的数量 */
    $sql = "UPDATE " .$GLOBALS['os']->table('vote'). " SET ".
           "vote_count = vote_count + 1 ".
           "WHERE vote_id = '$vote_id'";
    $GLOBALS['db']->query($sql);

    /* 更新投票选项的数量 */
    $sql = "UPDATE " . $GLOBALS['os']->table('vote_option') . " SET " .
           "option_count = option_count + 1 " .
           "WHERE " . db_create_in($option_id, 'option_id');
    $GLOBALS['db']->query($sql);
}

?>